﻿using Client.ServerServices;
using System;
using System.ServiceModel;
using System.Windows;
using System.Windows.Controls;

namespace Client
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : UserControl
    {
        static public String username;
        private MainWindow mainWindow;
        public Login()
        {
            InitializeComponent();
            username = null;
        }

        public Login(MainWindow mainWindow)
        {
            InitializeComponent();
            this.mainWindow = mainWindow;
        }


        private void Login_Click(object sender, RoutedEventArgs e)
        {
            ServerServices.LoginRequest loginReq = new ServerServices.LoginRequest();
            loginReq.UserName = usernameTextbox.Text;
            loginReq.Password = passwordBox.Password;
            ServerServices.LoginServiceResponse response;

            try
            {

                response = mainWindow.client.LoginProcess(loginReq);
                
                /*if (MainWindow.ssc.State == System.ServiceModel.CommunicationState.Faulted)
                {
                    IServerServicesCallback callback = new TwoWayMethods();
                    InstanceContext context = new InstanceContext(callback);
                    using (ServerServicesClient client = new ServerServicesClient(context))
                    {
                    }

                }
                response = MainWindow.ssc.LoginProcess(loginReq);*/
                if (response.Code == 101)
                {
                    username = loginReq.UserName;
                    if (MessageBox.Show("Sikeres belépés") == MessageBoxResult.OK)
                    {
                        if(response.Table != null)
                        {
                            mainWindow.SetScreenToGame(response.Table.TableID, response.Table.Turn, response.Table.Opponent,true);
                            Game gameUC = (Game)mainWindow.gameScreen;
                            gameUC.SetGame(response.Table);
                        }
                        else
                        {
                            mainWindow.SetScreen("lobby");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Sikertelen belépés hibakód: " + response.Code + " " + response.Message);
                }

            }
            catch (Exception)
            {
                mainWindow.client.Abort();
                mainWindow.GoToServer();
            }
        }

        private void RegistrationButton_Click(object sender, RoutedEventArgs e)
        {
            mainWindow.SetScreen("registration");
        }

        private void LoginAsGuest_Click(object sender, RoutedEventArgs e)
        {
            ServerServices.RegistrationRequest regReq = new ServerServices.RegistrationRequest();
            regReq.Username = "Guest";
            regReq.Password = "GuestAdminPassword";
            regReq.EmailAddress = "GuestAdminPassword@GuestAdminPassword.hu";
            ServerServices.RegistrationServiceResponse response;

            try
            {
                /*if (MainWindow.ssc.State == System.ServiceModel.CommunicationState.Faulted)
                {*/
                    /*IServerServicesCallback callback = new TwoWayMethods(mainWindow);
                    InstanceContext context = new InstanceContext(callback);
                    using (ServerServicesClient client = new ServerServicesClient(context))
                    {*/
                        response=mainWindow.client.RegistrationProcess(regReq);
                    //}
                //}
                //response = MainWindow.ssc.RegistrationProcess(regReq);
                if (response.Code >= 1000)
                {
                    username = "Guest_" + response.Code;
                    //mainWindow.SetScreen("lobby");

                    ServerServices.LoginRequest loginReq = new ServerServices.LoginRequest();
                    loginReq.UserName = username;
                    loginReq.Password = regReq.Password;
                    ServerServices.LoginServiceResponse loginResponse;

                    try
                    {

                        loginResponse = mainWindow.client.LoginProcess(loginReq);


                        if (loginResponse.Code == 101)
                        {
                            username = loginReq.UserName;
                            if (MessageBox.Show("Sikeres belépés") == MessageBoxResult.OK)
                            {
                                if (loginResponse.Table != null)
                                {
                                    mainWindow.SetScreenToGame(loginResponse.Table.TableID, loginResponse.Table.Turn, loginResponse.Table.Opponent, true);
                                    Game gameUC = (Game)mainWindow.gameScreen;
                                    gameUC.SetGame(loginResponse.Table);
                                }
                                else
                                {
                                    mainWindow.SetScreen("lobby");
                                }
                            }
                        }
                        else
                        {
                            MessageBox.Show("Sikertelen belépés hibakód: " + loginResponse.Code + " " + loginResponse.Message);
                        }

                    }
                    catch (Exception)
                    {
                        mainWindow.client.Abort();
                        mainWindow = new MainWindow();

                    }

                }
                else
                {
                    MessageBox.Show("Sikertelen belépés. Hibakód: " + response.Code + " " + response.Message);
                }

            }
            catch (Exception)
            {
                MessageBox.Show("Sikertelen belépés. szerverhiba");
                mainWindow.client.Abort();
                mainWindow.GoToServer();
            }
        }
    }
}
